package Master;

import java.util.ArrayList;
import java.util.Collections;

/*
 * TaskPlan class. Provides a plan of tasks input from
 * ClientGUI.
 */
public class TaskPlan implements Comparable{

	/* The ID of the Task Plan*/
	private int taskPlanID;
	
    /* The list of tasks input from xml files */
    private ArrayList<Task> tasks;

    /* The list of worker resources */
    private ArrayList<Resource> resources;

    /* The budget of this task plan, and the unit is a dollar */
    private float budget;

    /*
     * The task plan must be finished less than this time,
     * and the unit is a second.
     */
    private float timeRequired;
    
    /* The file path of the task plan */
    private String filepath;
    
    /*
     * The priority of the task plan 
     */
    private int priority;

    /*
     * Constructor
     */
    public TaskPlan(int taskPlanID, float budget, float timeRequired, 
    		String filepath){
        this.budget = budget;
        this.timeRequired = timeRequired;
        this.taskPlanID = taskPlanID;
        this.filepath = filepath;
        
        this.tasks = new ArrayList<Task>();
        this.resources = new ArrayList<Resource>();
        
        this.priority = 1;
    }

    public float getBudget(){
        return budget;
    }

    public float getTimeRequired(){
        return timeRequired;
    }

    public void addTask(Task task) {
        this.tasks.add(task);
    }

    public void addResource(Resource resource){
        this.resources.add(resource);
    }

    public ArrayList<Task> getTasks(){
        return tasks;
    }

    public ArrayList<Resource> getResources(){
        return resources;
    }

    @SuppressWarnings("unchecked")
	public void sortResources(){
        Collections.sort(this.resources);
    }
    
    public Resource getFirstResource(){
        // find the first resource which is available
        for(Resource resource : resources){
            if(resource.isAvailable()){
                return resource;
            }
        }
    	return null;
    }

	public int getTaskPlanID() {
		return taskPlanID;
	}

	public String getFilepath() {
		return filepath;
	}
	
	public void setBudget(float budget) {
		this.budget = budget;
	}

	public void setTimeRequired(float timeRequired) {
		this.timeRequired = timeRequired;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}
	
	public int compareTo(Object object) {
		// TODO Auto-generated method stub
		TaskPlan anotherTaskPlan = (TaskPlan)object;
		int priorityDis = anotherTaskPlan.getPriority() - this.getPriority();
		if(priorityDis > 0) return -1;
		else if(priorityDis < 0) return 1;
		else{
			if(anotherTaskPlan.getTimeRequired() != this.getTimeRequired()) return 0;
			int idDis = anotherTaskPlan.getTaskPlanID() - this.getTaskPlanID();
			if(idDis > 0) return -1;
			else if(idDis < 0) return 1;
		}
		return 0;
	}
}
